package com.ruanyun.aliyun;

import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.alibaba.fastjson.JSONObject;
import com.ruanyun.sys.core.exception.enums.BizExceptionEnum;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/ali")
public class AliUploadController {

    @RequestMapping("/uploadFile")
    @ResponseBody
    public Map uploadService(@RequestPart("file") MultipartFile picture) {
        JSONObject resObj = new JSONObject();
        Map map = new HashMap();
        String pictureURL = "";
        try {
            OSSUtil ossUtil = new OSSUtil();
            pictureURL = ossUtil.upload(picture);
        } catch (Exception e) {
            resObj.put("msg", "error");
            throw new ServiceException(BizExceptionEnum.UPLOAD_ERROR);
        }
        resObj.put("msg", "http://mangfengapp.oss-cn-beijing.aliyuncs.com/"+pictureURL);

        map.put("code", 0);
        map.put("data","http://mangfengapp.oss-cn-beijing.aliyuncs.com/"+pictureURL);
        return map;
    }
}
